<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of upfile
 *文件处理类
 * @author martin
 */
class upfile {
        public static function thumbs($params)
        {
            $thubmbs = ereg_replace('albums','thumbs',$params);
            if(file_exists(substr($thubmbs,1)))
                return $thubmbs;
            else
                return Yii::app()->request->baseUrl.'/images/noimage.png';
        }
        
        public static function deleteThumbsFile($link)
        {
           $thumbs = str_replace('albums', 'thumbs', $link); 
            @unlink(substr($thumbs, 1));
            @unlink(substr($link, 1));           
        }
        



        /**
         *删除旧文件
         * @param type $params file url
         */
        public static function deleteFile($params)
        {
            $params = substr($params,1);
            if(file_exists($params))
            {
                unlink($params);
            }
        }
        
        /**
         *保存LOGO
         * @param type $model
         * @param type $file
         * @return type route
         */
        public static function saveLogo($model,$file)
        {       
            $model->attributes = $_POST['MobileBrand'];
            $model->logo = CUploadedFile::getInstance($model,'logo');
            if($model->logo)
            {
                $newName = date('Ymd-His').'.'.$model->logo->extensionName;
            }

                $root = YiiBase::getPathOfAlias('webroot').Yii::app()->getBaseUrl();
                $folder = '/attachment/logo/';

                if(!is_dir($root.$folder))
                {
                    if(!mkdir($root.$folder, 0, true))
                    {
                        throw new Exception('创造文件夹失败...');
                    }
                }                  
                
                
                if($model->logo->saveAs($root.$folder.$newName))
                    return $folder.$newName;
                    
        }
        
        public static function saveThumbsFile($id,$file=array())
        {
                $model = new ViUpfile;
                if(isset($file['url']))
                {
                    $model->attributes = $file['url'];
                    $model->url = CUploadedFile::getInstance($model,'url');
                    if($model->url)
                    {
                        $newName = date('Ymd-His').'.'.$model->url->extensionName;
                    }
                    if(0 < $newName)
                    {
                        $criteria = new CDbCriteria;
                        $criteria->addCondition('content_id='.$id);
                        $del = ViUpfile::model()->find($criteria);
                        if(!is_null($del))
                        {
                            $thumbs = str_replace('albums', 'thumbs', $del->url);
                            @unlink(substr($thumbs, 1));
                            @unlink(substr($del->url, 1));
                            $del->delete();
                        }
                        $root = YiiBase::getPathOfAlias('webroot').Yii::app()->getBaseUrl();
                        $folder = '/attachment/albums/';

                        if(!is_dir($root.$folder))
                        {
                            if(!mkdir($root.$folder, 0, true))
                            {
                                throw new Exception('创造文件夹失败...');
                                }
                        }                  

                        if($model->url->saveAs($root.$folder.$newName))
                        {
                            //生成缩略图
                            $model->url = Yii::app()->getBaseUrl().$folder.$newName;
                            $model->ctime = $model->mtime = time();
                            $model->type_id = '0';
                            $model->content_id = $id;
                            $model->save();
                            $thumb=Yii::app()->phpThumb->create($root.'/attachment/albums/'.$newName);
                            $thumb->resize(100,100);
                            $thumb->save($root.'/attachment/thumbs/'.$newName);
                        }
                    }
                    
                }
        }
        
        /**
         *写入临时表
         * @param type $id = $order->id
         * @param type $params = order[content]
         */
        public static function createImgTemp($id,$params)
        {
                $model = ViTempAttachment::model()->findByPk($id);
                if($model === null)
                {
                    $res = array();
                    preg_match_all('/src=[\"\']?([%+\*\w\/:\._-]+(?:jpg|gif|bmp|jpeg|png))/ism',$params, $arr);
                    foreach($arr as $key)
                    {
                       $res[] = $key;
                    }
                    $model = new ViTempAttachment;
                    $model->content = serialize($res[0]);
                    $model->id = $id;
                    $model->user_id = Yii::app()->user->id;
                    $model->attributes = $model;
                    $model->save();
                }

        }
        
        /**
         *删除内容不存在的图片
         * @param type $id = $order->id
         * @param type $params =  order[content]
         */
        public static function deleteImgTemp($id,$params=array())
        {
            $model = ViTempAttachment::model()->findByPk($id);
            if($model == true && $model->user_id == Yii::app()->user->id)
            {
                 preg_match_all('/src=[\"\']?([%+\*\w\/:\._-]+(?:jpg|gif|bmp|jpeg|png))/ism',$params, $arr);
                 $result = array_diff(unserialize($model->content), $arr[0]);
                 foreach($result as $key)
                 {
                     $file = str_replace('src="/', '', $key);
                     if(file_exists($file))
                     {
                         unlink($file);
                     }
                 }
                $model = ViTempAttachment::model()->findByPk($id);
                $model->delete();
            }
        }
        
        public static function deleteContentImg($id,$params=array())
        {
            preg_match_all('/src=[\"\']?([%+\*\w\/:\._-]+(?:jpg|gif|bmp|jpeg|png))/ism',$params, $arr);

             foreach($arr[0] as $key)
             {
                 $file = str_replace('src="/', '', $key);
                 if(file_exists($file))
                 {
                    unlink($file);
                 }
             }
        }
}

?>
